<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

class m_medias_actions_block_DashBoardMediasAction extends m_backoffice_extends_Action
{
	public function execute()
	{
            //medias infos
            $documentProvider = f_document_Provider::getInstance();
            $nbMedia = $documentProvider->loadDocuments("modules_medias/media")->count();

            $this->addToModel('nbMedia', $nbMedia);

            //get all media folder
            $documentProvider = f_document_Provider::getInstance();
            $mediasFolder = $documentProvider->loadDocuments("modules_backoffice/folder")->whereIsEqual("module", "medias")->retrieveIds();

            $totalSize = 0;
            foreach($mediasFolder as $folder)
            {

                if(is_dir('..'.DS.'images'.DS.$folder) and $handle = opendir('..'.DS.'images'.DS.$folder)) {
                    while (false !== ($file = readdir($handle))) {

                        if($file != '.' and $file != '..')
                        if(is_dir('..'.DS.'images'.DS.$folder.DS.$file) and $handle2 = opendir('..'.DS.'images'.DS.$folder.DS.$file)) {
                                while (false !== ($file2 = readdir($handle2))) {
                                    if($file2 != '.' and $file2 != '..')
                                    $totalSize += filesize('..'.DS.'images'.DS.$folder.DS.$file.DS.$file2);
                                }
                                closedir($handle2);
                            }


                    }
                    closedir($handle);
                }
            }
            $totalSize = round($totalSize/1024/1024, 2); //Mo

            //Disk percentage
            $totalspace = disk_total_space("./")/1024/1024; //Mo
            $percent_use = round(($totalSize*100)/$totalspace, 2);
            $percent_free = round(100-$percent_use, 2);

            $this->addToModel('percent_use', $percent_use);
            $this->addToModel('percent_free', $percent_free);
            $this->addToModel('totalSize', $totalSize);
            $this->setView("success");
	}

	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "backoffice_accessMedias"));
	}
}